package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.StbCompany;
import com.ruoyi.system.domain.StbEmployee;
import com.ruoyi.system.domain.StbPolicy;
import com.ruoyi.system.service.IStbPolicyService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 政策条例Controller
 * 
 * @author ruoyi
 * @date 2020-02-08
 */
@Controller
@RequestMapping("/system/policy")
public class StbPolicyController extends BaseController
{
    private String prefix = "system/policy";

    @Autowired
    private IStbPolicyService stbPolicyService;

    @RequiresPermissions("system:policy:view")
    @GetMapping()
    public String policy()
    {
        return prefix + "/policy";
    }
    
	/*
	 * 查询政策  policyname:政策内容  policydetail: 
	 */
    @PostMapping("/policy")
    @ResponseBody
    public List<Map<String,Object>> selectPolicyContent(){
        StbPolicy stbPolicy = new StbPolicy();
        List<Map<String, Object>> policycontentList = stbPolicyService.selectPolicyContent(stbPolicy);
        return policycontentList;
    }

    /**
     * 查询政策条例列表
     */
    @RequiresPermissions("system:policy:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(StbPolicy stbPolicy)
    {
        startPage();
        List<StbPolicy> list = stbPolicyService.selectStbPolicyList(stbPolicy);
        return getDataTable(list);
    }

    /**
     * 导出政策条例列表
     */
    @RequiresPermissions("system:policy:export")
    @Log(title = "政策条例", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StbPolicy stbPolicy)
    {
        List<StbPolicy> list = stbPolicyService.selectStbPolicyList(stbPolicy);
        ExcelUtil<StbPolicy> util = new ExcelUtil<StbPolicy>(StbPolicy.class);
        return util.exportExcel(list, "policy");
    }

    /**
     * 新增政策条例
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存政策条例
     */
    @RequiresPermissions("system:policy:add")
    @Log(title = "政策条例", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(StbPolicy stbPolicy)
    {
    	StbPolicy obj= new StbPolicy();
    	obj.setPolicyName(stbPolicy.getPolicyName());
    	List<StbPolicy> policyList= stbPolicyService.selectStbPolicyList(obj);
    	if(policyList!=null&&policyList.size()>0) {
    		AjaxResult json = new AjaxResult();
	        json.put("msg", "政策名称已存在");
	        json.put("code", 500);
	        return json;
    	}
    	stbPolicy.setCreateBy(ShiroUtils.getLoginName());
    	stbPolicy.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(stbPolicyService.insertStbPolicy(stbPolicy));
    }

    /**
     * 修改政策条例
     */
    @GetMapping("/edit/{policyId}")
    public String edit(@PathVariable("policyId") Long policyId, ModelMap mmap)
    {
        StbPolicy stbPolicy = stbPolicyService.selectStbPolicyById(policyId);
        mmap.put("stbPolicy", stbPolicy);
        return prefix + "/edit";
    }

    /**
     * 修改保存政策条例
     */
    @RequiresPermissions("system:policy:edit")
    @Log(title = "政策条例", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(StbPolicy stbPolicy)
    {
    	StbPolicy obj= new StbPolicy();
    	obj.setPolicyName(stbPolicy.getPolicyName());
    	List<StbPolicy> policyList= stbPolicyService.selectStbPolicyList(obj);
    	stbPolicy.setCreateBy(ShiroUtils.getLoginName());
    	stbPolicy.setUpdateBy(ShiroUtils.getLoginName());
    	int flag=0;
    	if(policyList==null&&policyList.size()==0) {
    		flag=1;
    	}else if(policyList.size()==1) {
    		StbPolicy stbPolicy2 = policyList.get(0);
    		if(stbPolicy2.getPolicyId().equals(stbPolicy.getPolicyId())) {
    			flag=1;
    		}else {
    			flag=2;
    		}
    	}
    	AjaxResult json = new AjaxResult();
    	if(flag==1) {
        return toAjax(stbPolicyService.updateStbPolicy(stbPolicy));
    	}else if(flag==2) {
    		json.put("msg", "政策名称已存在");
    	}else {
    		json.put("msg", "政策名称一经确定不可更改!");
    	}
    	json.put("code", 500);
    	return json;
    }

    /**
     * 删除政策条例
     */
    @RequiresPermissions("system:policy:remove")
    @Log(title = "政策条例", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(stbPolicyService.deleteStbPolicyByIds(ids));
    }
}
